
/**
 * 商品搜索
 *    1. 获取商品关键字
 *    2. 根据关键字查询商品详情接口
 *    3. 动态渲染相关商品数据
 */
//  1. 获取商品关键字
function getValue(){
    let url=location.href
    let val=url.indexOf('?')
    val=url.substring(val+1)
    let keyword=val.split('=')
  return   keyword=decodeURI(keyword[1]) 
}
getValue()
let keyword=getValue()
//调取接口
function getsearch(){
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/search',
        params:{
            keyword
        }
    }).then(response=>{
        let data=response.data
        console.log('111' , data);
        if(data.resultCode==1){
            let list=data.resultInfo.list
            console.log(list);
            showlist(list)
        }
    })
}
getsearch()

//渲染对应商品
function showlist(list){
    let str = list.map(item => {
        return ` <div class="product" onclick="toDetail(${item.id})" >
                    <img src="${item.picture}" alt="">
                    <div class="seleproduct">
                        <p>${item.shop}</p>
                        <p><span>￥${item.price}</span> 1人买</p>
                    </div>
                </div>`
    }).join('')
   $('.content').html(str)
}
//在搜索页面搜索商品
function searchlist(){
    $('input[name="pt"]').on('click', function (e) {
        e.preventDefault()
        let keyword= $('input[name="ps"]').val()
        axios({
            method:'get',
            url:'http://api.yuguoxy.com/api/shop/search',
            params:{
                keyword
            }
        }).then(response=>{
            let data=response.data
            console.log('111' , data);
            if(data.resultCode==1){
                let list=data.resultInfo.list
                console.log(list);
                showlist(list)
            }
        })
    })  
}
searchlist()
//返回首页
function blanck(){
    $('header>button').on('click', function () {
        location.href = '../pages/index.html'
    })
}
blanck()